package com.example.dell.fastshopping.user.task;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.google.gson.GsonBuilder;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by ${YSH} on 2019/5/9.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class JoinViewModel extends ViewModel {
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<CodeInfo> qrcode = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> jihuo = new MutableLiveData<>();

    public MutableLiveData<CodeInfo> getQrCode(int type) {
        RetrofitClient.execute(service.getQrCode(type), new NetCallback<BaseResponse<CodeInfo>>() {
            @Override
            public void netCallback(BaseResponse<CodeInfo> response) {
                qrcode.setValue(response.getData());
            }
        });
        return qrcode;
    }

    public MutableLiveData<BaseResponse> checkJihuo(int type, String code) {
        // groupType-1:qq群 2:微信群 3:公众号   yzm-验证码
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("groupType",type);
        hashMap.put("yzm",code);
        String json = new GsonBuilder().create().toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        RetrofitClient.execute(service.checkYZM(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse response) {
                jihuo.setValue(response);
            }
        });
        return jihuo;
    }
}
